jQuery(function(){
	jQuery.updnWatermark.attachAll();
});

/* Main Menu >>>*/
jQuery(function() {
	 // hides the collectionDetail as soon as the page load
	  jQuery('.lang-list').hide();
	 // shows the collectionDetail on clicking each product thumb
	  jQuery('#langSelector').click(function() {
		jQuery('.lang-list').toggle("slow");
		return false;
	  });
	 // hides the collectionDetail on clicking the closeBtn
	  jQuery('.lang-item').click(function() {
		jQuery('.lang-list').hide("slow");
		return false;
	  });
});

 jQuery(function () {

  	jQuery(".category_menu .menu").append("<div id=\"flyout-arrow\"><div id=\"flyout-arrow-tip\"></div></div>");
  	jQuery("#flyout-arrow").css("left", jQuery("#category_menu .level0item:first").position().left + jQuery("#category_menu .level0item:first").width() / 2);
  	jQuery("#flyout-arrow-tip").stop().animate({
  		height: "10px"
  	}, 120);
  	jQuery("#category_menu .flyout").addClass("flyout-enabled");
  	var ulWrappers = jQuery("#category_menu .category").find(".ul_wrapper");
  	jQuery.each(ulWrappers, function (intIndex) {
  		var maxSpaceV = jQuery("#category_menu .flyout").height();
  		var availableSpaceV = maxSpaceV;
  		var column = 1;
  		jQuery.each(jQuery(this).find(".2level"), function (intIndex) {
  			availableSpaceV -= jQuery(this).outerHeight(true);
  			if (availableSpaceV < 0) {
  				column++;
  				availableSpaceV = maxSpaceV;
  				availableSpaceV -= jQuery(this).outerHeight(true);
  				jQuery(this).addClass("reset");
  			}
  			jQuery(this).addClass("column" + column);
  		});
  		for (var i = 1; i <= (column + 1); i++) {
  			var totalHeight = 0;
  			for (var k = 0; k < (i - 1); k++) {
  				jQuery.each(jQuery(this).find(".column" + k), function (intIndex) {
  					totalHeight += jQuery(this).outerHeight(true);
  				});
  			}
  			totalHeight *= -1;
  			jQuery(this).find(".column" + (i - 1)).css("top", totalHeight);
  		}
  	});

  	var object;
  	var wrapper;
  	for (var i = jQuery("#category_menu .flyout .2level").length; i > 0; i--) {
  		wrapper = jQuery("#category_menu .flyout .2level").eq(i - 1);
  		wrapper.css("top", wrapper.position().top);
  		for (var k = wrapper.find(".level2item").length; k > 0; k--) {
  			object = wrapper.find(".level2item").eq(k - 1);
  			object.css("top", object.position().top);
  			object.addClass("column-fixed");
  		}
  		wrapper.addClass("column-fixed");
  	}
  	jQuery("#category_menu .flyout").removeClass("flyout-enabled");

  	var activeLi = null;
  	var nextLi = null;

  	jQuery("#category_menu .level0item").mouseover(function () {
  		if (jQuery(this).attr("rel") != "0") {
  			activeLi = jQuery(this).attr("rel");
  			jQuery('#category_menu .level0item').unbind();

  			<!-- initiate systems -->			
  			jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").removeClass('flyout-disabled').addClass('flyout-enabled');
  			jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").css("opacity", "1");
  			jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-disabled').addClass('flyout-enabled');
  			var triggerLi = jQuery(this);
  			jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").stop().animate({
  				opacity: 1
  			}, 120, function () {
  				jQuery("#flyout-arrow").css("display", "block");
  				jQuery("#flyout-arrow").css("left", (triggerLi.position().left + triggerLi.width() / 2 - jQuery("#flyout-arrow").width() / 2));
  				jQuery("#flyout-arrow").css("opacity", 1);
  				jQuery("#flyout-arrow-tip").css("opacity", 1);
  			});

  			jQuery("#category_menu .level0item").mouseenter(function () {
  				if (jQuery(this).attr("rel") != "0") {
  					jQuery("#category_menu .level0item .flyout").stop(true, true);
  					jQuery("#category_menu .level0item .flyout .flyout-background").stop(true, true);
  					if (activeLi != null) {
  						if (jQuery(this).attr("rel") != activeLi) {
  							if (nextLi) {
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout .flyout-background").stop();
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout .flyout-background").css("opacity", "0");
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").addClass('flyout-enabled').removeClass('flyout-disabled');
  								nextLi = jQuery(this).attr("rel");
  								jQuery("#category_menu .level0item:not([rel=\"" + nextLi + "\"]) .flyout").removeClass('flyout-enabled').addClass('flyout-disabled');
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").css("opacity", "1");
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").addClass('flyout-enabled').removeClass('flyout-disabled');

  							} else {
  								nextLi = jQuery(this).attr("rel");
  							}
  							jQuery("#flyout-arrow").stop().animate({
  								opacity: 1,
  								'left': (jQuery(this).position().left + jQuery(this).width() / 2 - jQuery("#flyout-arrow").width() / 2)
  							}, 120);
  							jQuery("#flyout-arrow-tip").stop().animate({
  								opacity: 1,
  								height: "10px"
  							}, 120, function () {
  								jQuery("#category_menu .level0item .flyout .flyout-background").stop(); 
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").stop().animate({
  									opacity: 0
  								}, 120, function () {
  									jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout .flyout-background").css("opacity", "0");
  									jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").addClass('flyout-enabled').removeClass('flyout-disabled');
  									jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").css("opacity", "1");
  									jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-enabled').addClass('flyout-disabled');
  									jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").css("opacity", "0");
  									jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout .flyout-background").stop().animate({
  										opacity: 1
  									}, 120, function () {
  										activeLi = nextLi;
  										nextLi = null;
  									});
  								});
  							});
  						} else {
  							jQuery("#flyout-arrow").stop().animate({
  								opacity: 1,
  								'left': (jQuery(this).position().left + jQuery(this).width() / 2 - jQuery("#flyout-arrow").width() / 2)
  							}, 120);
  							jQuery("#flyout-arrow-tip").stop().animate({
  								opacity: 1,
  								height: "10px"
  							}, 120);
  							jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-disabled').addClass('flyout-enabled');
  							if (nextLi) jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout .flyout-background").stop().animate({
  								opacity: 0
  							}, 120, function () {
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-disabled').addClass('flyout-enabled');
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").css("opacity", "1");
  								jQuery("#category_menu .level0item[rel=\"" + nextLi + "\"] .flyout").addClass('flyout-disabled').removeClass('flyout-enabled');
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").css("opacity", "0");
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").stop().animate({
  									opacity: 1
  								}, 120, function () {
  									nextLi = null;
  								});
  							});
  							else {
  								jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").stop().animate({
  									opacity: 1
  								}, 120, function () {
  									nextLi = null;
  								});
  							}
  						}
  					} else {
  						activeLi = jQuery(this).attr("rel");
  						jQuery("#flyout-arrow").css("left", (jQuery(this).position().left + jQuery(this).width() / 2 - jQuery("#flyout-arrow").width() / 2));
  						jQuery("#flyout-arrow-tip").stop().animate({
  							opacity: 1,
  							height: "10px"
  						}, 120);
  						jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-disabled').addClass('flyout-enabled');;
  						jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout .flyout-background").css("opacity", "1");
  						jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").stop().animate({
  							opacity: 1
  						}, 120);
  					};
  				}

  			});

  			jQuery("#category_menu").mouseleave(function () {
  				jQuery("#category_menu .level0item .flyout .flyout-background").stop();
  				jQuery("#flyout-arrow-tip").stop().animate({
  					opacity: 0,
  					height: "0px"
  				}, 120, function () {
  					jQuery("#category_menu .level0item .flyout").stop().animate({
  						opacity: 0
  					}, 120, function () {
  						jQuery("#category_menu .level0item .flyout").addClass('flyout-disabled').removeClass('flyout-enabled');
  						jQuery("#category_menu .level0item .flyout .flyout-background").css("opacity", "0");
  						jQuery("#category_menu .level0item .flyout .flyout-background").stop();
  						activeLi = null;
  					});
  				});
  			});
  			jQuery("#category_menu .level0item[rel=\"" + activeLi + "\"] .flyout").removeClass('flyout-disabled').addClass('flyout-enabled');
  		}
  	});

 (function (jQuery) {
  		jQuery.extend(jQuery.fx.step, {
  			backgroundPosition: function (fx) {
  				if (fx.state === 0 && typeof fx.end == 'string') {
  					var start = jQuery.curCSS(fx.elem, 'backgroundPosition');
  					start = toArray(start);
  					fx.start = [start[0], start[2]];
  					var end = toArray(fx.end);
  					fx.end = [end[0], end[2]];
  					fx.unit = [end[1], end[3]];
  				}
  				var nowPosX = [];
  				nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
  				nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
  				fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

  				function toArray(strg) {
  					strg = strg.replace(/left|top/g, '0px');
  					strg = strg.replace(/right|bottom/g, '100%');
  					strg = strg.replace(/([0-9\.]+)(\s|\)|jQuery)/g, "jQuery1pxjQuery2");
  					var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
  					return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
  				}
  			}
  		});
  	})(jQuery);
  });

/*////*/

/* overlays >>>
******************************************************************************/

  var $overlays_groups = $('.overlay-group')
  
  $overlays_groups.each(function() {
    var $group = $(this)
    $group.find('.overlay-block a').each(function() {
      var $block = $(this)
      var $overlay = $('<div class="overlay"></div>')
      $overlay.data('link', $block.find('a').eq(0))
      
      //if($(this).attr('id') != 'img_concour'){
       // $overlay.click(function(){
         // window.location = $(this).data('link').attr('href')
        //})
      //}
      
      $block.prepend($overlay)
      $overlay.data('group', $group)
      $overlay.hover(overlayBlockHandleIn, overlayBlockHandleOut)
    })
    $group.hover(overlayGroupHandleIn, overlayGroupHandleOut)
  })
  
  function overlayBlockHandleIn() {
    var $overlay = $(this)
    $overlay.data('group').find('.overlay').stop().not($overlay).animate({opacity:0.3}, {duration:400, queue:false})
    $overlay.animate({opacity:0}, {duration:400, queue:false})
  }
  
  function overlayBlockHandleOut() {
    var $overlay = $(this)
    $overlay.data('group').find('.overlay').stop().not($overlay).animate({opacity:0}, {duration:400, queue:false})
  }
  
  function overlayGroupHandleIn() {
    
  }
  
  function overlayGroupHandleOut() {
    var $group = $(this)
    $group.find('.overlay').animate({opacity:0}, {duration:400, queue:false})
  }

/* //// */

/* Gallery >>> */
var $gallery_pos = $('#gallery-pos');
  var $gallery_item_pos = $gallery_pos.find('li:first').remove().clone().removeClass('active');
  var $gallery = $('#gallery');
  var $items = $gallery.find('.item');
  var $arrows = $('#gallery-inner').find('.arrow');
  var $gallery_cursors = $('#gallery-pos').find('li');
  var jump = $items.first().width();
  var $itemToMove, $itemCopy, direction, tmpJump;
  var timer;

	
	
  if($gallery.length) {
    if($items.length>1/* && $('body').hasClass('section-accueil')*/){
		timer=setInterval(function(){
						 	galleryArrowClick(1);						
						 },5000);
	}
	
	$arrows.click(function(e){
		e.preventDefault();
		if(timer){
			clearInterval(timer);
		}
		var direction = $(this).data('direction');
		galleryArrowClick(direction);			   
	});
    
    $(document).keydown(handleKeyDown)
    initGalleryCursor();
    $('#sub-section-nav li:first a:first').trigger('click')
  }
  
  if(location.hash != ""){
    var ref = location.hash;
    $("a[href='"+ref+"']").trigger('click');
  }
  
  function galleryArrowClick(direction) {
    if(!$gallery.is(':animated')) {
      $items = $gallery.find('.item');

      if(direction == 1) {
        if($gallery.offset().left + $items.first().offset().left < 0) {
          $gallery.append($items.first())
          $gallery.animate({
            left: '+=' + jump * direction
          }, 0)
        }
      }
      else {
        if($gallery.offset().left + $items.last().offset().left > 0) {
          $gallery.prepend($items.last())
          $gallery.animate({
            left: '+=' + jump * direction
          }, 0)
        } 
      }
      
      $gallery.animate({
        left: '-=' + jump * direction
      }, 1000, animateGalleryCallback)

      handleGalleryCursor(direction);
    }

    return false
  }
  
  
  function resetGallery() {
    var $gallery_wrap = $('#gallery-wrap');
    $items = $gallery.find('.item');
    $gallery_wrap.show();
    $arrows.show();
    $gallery.css('left', 0);
    
    if($items.length == 0) {
      $gallery_wrap.hide();
    } else if($items.length == 1) {
      $arrows.hide();
    }
  }
  
  function initGalleryCursor() {
    var $items = $gallery.find('.item');
    $gallery_pos.empty();
    
    for(var i = 0; i < $items.length; i++) {
      $gallery_pos.append($gallery_item_pos.clone());
    }
    
    $gallery_pos.find('li:first').addClass('active');
    
    $gallery_cursors = $('#gallery-pos').find('li');
  }

  function animateGalleryCallback() {
    
  }

  function handleGalleryCursor(direction) {
    var $current = $gallery_cursors.filter('.active');
    var $next_active = $current.prev();
  
  
    if(direction == 1) {
      $next_active = $current.next();
      if($next_active.length < 1) {
        $next_active = $gallery_cursors.first();
      }
    }
    else if($next_active.length < 1) {
      $next_active = $gallery_cursors.last();
    }

    $gallery_cursors.removeClass('active');
    $next_active.addClass('active');
  }

  function handleKeyDown(e) {
    if(e.which == 37) {
      $arrows.filter('.prev').click();
    }
    else if(e.which == 39) {
      $arrows.filter('.next').click();
    }
  }

/* /// */

jQuery("#productFilter").hide();
	jQuery('form#filterForm select').each(function() {
	  jQuery(this).change(function() {
		jQuery('form#filterForm').submit();
	  })
	})
	
	$('select:not(#concour-wrapper select.customSelect)').selectBox({
		autoWidth: false
	}) 
    

/*// Accordion//*/

jQuery(function() {
	$( "#accordion" ).accordion({
		collapsible: true
	});
});

/*--- Zoom in product detail---*/

jQuery(function(){
	jQuery(".more").colorbox({inline:true});
});

MagicZoomPlus.options = {
									  
  'zoom-width':'0',
  'zoom-height':'0',

	'expand-effect':'linear',
	'background-opacity':'0',
	'expand-speed':'200',
	'expand-speed':'200',
	'disable-expand':false,
	'disable-zoom':false,
	'thumb-change':'click',
	'buttons-display':'close'
}


  